package com.wenjiang.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.wenjiang.common.annotation.RequireToken;
import com.wenjiang.common.dto.OrderParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.service.MallOrderService;
import com.wenjiang.common.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lwj
 * @since 2021-07-20
 */
@RestController
@RequestMapping("/mallOrder")
public class MallOrderController {

    @Autowired
    MallOrderService mallOrderService;

    //创建订单

    @RequestMapping("/createdOrder")
    @RequireToken
    public  Object createdOrder(HttpServletRequest request ,@RequestBody OrderParam orderParam) throws Exception {

        String token= request.getHeader("token");
        Long userId = JWTUtil.getUserId(token);

        Result result = mallOrderService.createCartOrder(orderParam,userId.toString());

        return result;

    }


    //获取用户全部订单
    @RequestMapping("/getUserOrder")
    @RequireToken
    public  Object getUserOrder(HttpServletRequest request) throws JsonProcessingException {

        String token= request.getHeader("token");
        Long userId = JWTUtil.getUserId(token);

        Result result = mallOrderService.getUserOrder(userId);

        return result;

    }





}
